package com.Test.HomeWork;

import java.io.*;
import java.util.ArrayList;

/**
 * 2、 定义一个Person类 包含name,age,sex三个属性，然后序列化和反序列化Person类对象，最后代码运行结果如下：
 *   Person对象序列化成功！
 *   Person对象反序列化成功！
 *   name=wangxiaoming,age=18,sex=男
 */

class People implements Serializable{
    public static long serialVersionUID=0l;
    private String name;
    private int age;
    private String sex;

    public People(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "\nPeople{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}
public class work2 {

    public static void main(String[] args) throws Exception {
       // SerializableT();
        SerializableT2();

    }
    public static ArrayList<People> SerializableT() throws IOException {
        ArrayList<People> people = new ArrayList<>();
        People people1 = new People("wangxiaoming", 18, "男");
        People people2 = new People("wangxiaoming", 18, "男");
        People people3 = new People("wangxiaoming", 18, "男");
        People people4 = new People("wangxiaoming", 18, "男");
        people.add(people1);
        people.add(people2);
        people.add(people3);
        File file=new File("G:\\培训班\\代码块\\week4\\day2\\src\\a2.txt");
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream(file));
        objectOutputStream.writeObject(people);
        objectOutputStream.close();
        return people;


    }
    public static void SerializableT2() throws Exception {

        File file=new File("G:\\培训班\\代码块\\week4\\day2\\src\\a2.txt");
        ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream(file));
         //= new ArrayList<>();

        ArrayList<People> people= (ArrayList<People>) objectInputStream.readObject();
        System.out.println(people.toString());

        objectInputStream.close();



    }
}
